/**
 * 
 */
package com.angis.fx.activity.search;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.angis.fx.activity.Enforcement;
import com.angis.fx.activity.R;
import com.angis.fx.activity.login.LoginActivity;
import com.angis.fx.data.AreaType;
import com.angis.fx.data.ChangsuoType;
import com.angis.fx.data.SearchCondition;
import com.angis.fx.util.SpinnerUtil;

/**
 * @author Han
 * 
 */
public class FieldSearchActivity extends Activity {
	
	private Spinner mAreaTypes1;
	
	private Spinner mAreaTypes2;
	
	private EditText mCsName;
	private EditText mAddresse;
	private EditText mPermitCode;
	private EditText mPermitword;
	private Spinner mCsTypes;
	private Button mSearchButton;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fieldsearch);
		
		initActivity();
		
		mAreaTypes1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				SpinnerUtil.setChildAreaCodes(FieldSearchActivity.this, mAreaTypes2, mAreaTypes1.getSelectedItem().toString());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				SpinnerUtil.setChildAreaCodes(FieldSearchActivity.this, mAreaTypes2, "");
				
			}
		});
		
		
		try {
			SpinnerUtil.setAreaCodes(this, mAreaTypes1);
			SpinnerUtil.setChangsuoType(this, (Spinner) findViewById(R.id.cstypevalue));
		} catch (XmlPullParserException e) {
			Toast.makeText(this, "配置文件读取错误", Toast.LENGTH_LONG);
		} catch (IOException e) {
			Toast.makeText(this, "配置文件读取错误", Toast.LENGTH_LONG);
		}

		mSearchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				

				SearchCondition lCondition = new SearchCondition();
				if (mCsName.getText() != null)
					lCondition.setConTitle(mCsName.getText().toString());
				if (mAddresse.getText() != null)
					lCondition.setAddress(mAddresse.getText().toString());
				if (mPermitCode.getText() != null)
					lCondition.setPermitCode(mPermitCode.getText().toString());
				if (mPermitword.getText() != null)
					lCondition.setPermitWord(mPermitword.getText().toString());
				if (mCsTypes.getSelectedItem() != null)
					lCondition.setCsType(mCsTypes.getSelectedItem().toString());
				if (mAreaTypes2.getSelectedItem() != null){
					
					lCondition.setAreaType(getAreaCode(mAreaTypes2.getSelectedItem()
							.toString()));
				}
				Intent lIntent = new Intent();
				lIntent.putExtra("searchcondition", lCondition);
				lIntent.setClass(FieldSearchActivity.this,
						BriefSearchResultActivity.class);
				FieldSearchActivity.this.startActivity(lIntent);
			}

			private String getAreaCode(String string) {
				for (AreaType lType : Enforcement.mAreaTypes){
					if (string.equals(lType.getName()))
						return lType.getCode();
				}
				return "";
			}
		});
	}

	private void initActivity() {
		mAreaTypes1 = (Spinner) findViewById(R.id.csareavalue1);
		mAreaTypes2 = (Spinner) findViewById(R.id.csareavalue2);
		mCsName = (EditText) findViewById(R.id.csnamevalue);
		mAddresse = (EditText) findViewById(R.id.csplacevalue);
		mPermitCode = (EditText) findViewById(R.id.cslicensenumbervalue);
		mPermitword = (EditText) findViewById(R.id.cslicensewordvalue);
		mCsTypes = (Spinner) findViewById(R.id.cstypevalue);
		mSearchButton = (Button) findViewById(R.id.fieldsearchbutton);
	}

	

}
